package com.algorithm.sort;

import java.util.Objects;

/**
 * @program: study
 * @className: SelectSort
 * @description: TODO
 * @author: kukuxiahuni
 * @create: 2020-01-02 17:13
 * @version: v1.0
 **/
public class SelectSort {

    /**
     * 功能描述:
     *
     * @param:
     * @return:
     * @auther: kukuxiahuni
     * @date: 2020/1/2 5:13 下午
     * @modify_auther: kukuxiahuni
     * @modify_time: 2020/1/2 5:13 下午
     **/
    public static void main(String[] args) {

        int[] array = {4, 5, 6, 3, 2, 1};
        selectSort(array);
    }

    /**
     * 功能描述: 选择排序
     *
     * @param:
     * @return:
     * @auther: kukuxiahuni
     * @date: 2020/1/3 11:03 上午
     * @modify_auther: kukuxiahuni
     * @modify_time: 2020/1/3 11:03 上午
     **/
    public static final void selectSort(int[] array) {

        if (Objects.isNull(array) || array.length <= 1) {
            return;
        }


        for (int i = 0; i < array.length - 1; ++i) {
            int value = array[i];
            int min = Integer.MAX_VALUE;
            int index = i;
            for (int j = i; j < array.length; ++j) {
                if (array[j] < min) {
                    min = array[j];
                    index = j;
                }
            }
            array[i] = array[index];
            array[index] = value;
        }

        for (int n : array) {
            System.out.println(n);
        }

    }
}
